<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
//引入验证码类
require_once app_path().'\Org\code\Code.class.php';
use App\Org\code\Code;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use App\Http\Model\Admin_user;

class LoginController extends Controller
{
   //加载登录模板
   public function login()
   {

       return view("admin.login");
   }
   
   /**
     * 生成验证码
     * @param 参数
     * @return 返回值
     * @author 作者
     * @date  2017.6.29  14:32
     */
    public function code()
    {
        $code  = new Code();
        return $code->make();
    }
		
		
      //登录处理
    public function dologin()
    {
        //dd(Input::all());
        //接受用户传过来的参数
        $input = Input::except('_token');
        //判断验证码是否正确
        $code = Session::get('code');
        //dd($input);
        if(strtoupper($input['mycode']) != $code){
            return back()->with('error','验证码错误');
        }


        //验证用户名
        $user = Admin_user::where('username',$input['username'])->first();


        if(!$user){
            return back()->with('error','此用户不存在');
        }
        
        
        //验证密码
       if($input['password'] != Crypt::decrypt($user->password)){
           return back()->with('error','密码不正确');
       }


        Session::put('usersession',$user);
        
        //重定向到后台首页
        return redirect('admin');


    }  
  
    
   
   
   //执行退出
   public function logout(Request $request)
   {
        session(['usersession'=>null]);
        session()->flush();
       return redirect("admin/login");
   }
   
   
}


